// Generic functions

function stringToNode(string){
	var parser = new DOMParser();
	return parser.parseFromString(string, "text/xml");
}

function appendErrorMessage(html){
	document.getElementById("errorMessages").innerHTML = html;
}

function destroyErrorMessage(){
	document.getElementById("errorMessages").innerHTML = "";
}

function jsonManage(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	if(json['status'] == 1){
		window.location = json['link'];
	}
}

// Specific functions

function registerAccount_response(xhr){
	appendErrorMessage(xhr.responseText);
	document.getElementById("inscriptionButton").disabled = false;
}

function forgotMail_response(xhr){
	appendErrorMessage(xhr.responseText);
	document.getElementById("forgottenButton").disabled = false;
}

function changePassword_response(xhr){
	document.getElementById("confirmButton").disabled = false;
	jsonManage(xhr);
}

function loginAccount_response(xhr){
	document.getElementById("loginButton").disabled = false;
	jsonManage(xhr);
}

function newMember_response(xhr){
	document.getElementById("newMemberButton").disabled = false;
	jsonManage(xhr);
}

function editMember_response(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	document.getElementById(json['id']).outerHTML = json['content'];
}

function deleteMember_response(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	document.getElementById(json['id']).outerHTML = "";
}

function makeMemberEdit_response(xhr){
	destroyErrorMessage();
	var json = JSON.parse(xhr.responseText);
	document.getElementById(json['id']).outerHTML = json['content'];
}

function makeColorEdit_response(xhr){
	destroyErrorMessage();
	var json = JSON.parse(xhr.responseText);
	document.getElementById("div"+json['id']).outerHTML = json['content'];
	createShape(json['id'],'createCircle',document.getElementById(json['id']+'redQuantity').value,document.getElementById(json['id']+'greenQuantity').value,document.getElementById(json['id']+'blueQuantity').value);
}

function makeShapeEdit_response(xhr){
	destroyErrorMessage();
	var json = JSON.parse(xhr.responseText);
	document.getElementById("div"+json['id']).outerHTML = json['content'];
	createShape(json['id'],json['javascriptFunction'],0,0,0);
}

function makeMoodEditForm_response(xhr){
	destroyErrorMessage();
	var json = JSON.parse(xhr.responseText);
	document.getElementById(json['id']).outerHTML = json['content'];
}

function editColor_response(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	document.getElementById("div"+json['id']).outerHTML = json['content'];
	createShape(json['id'],'createCircle',document.getElementById(json['id']+'redQuantity').innerHTML,document.getElementById(json['id']+'greenQuantity').innerHTML,document.getElementById(json['id']+'blueQuantity').innerHTML);
}

function editShape_response(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	document.getElementById("div"+json['id']).outerHTML = json['content'];
	createShape(json['id'],json['javascriptFunction'],0,0,0);
}

function deleteColor_response(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	document.getElementById('div'+json['id']).outerHTML = json['content'];
	createShape(json['id'],'createCircle',document.getElementById(json['id']+'redQuantity').innerHTML,document.getElementById(json['id']+'greenQuantity').innerHTML,document.getElementById(json['id']+'blueQuantity').innerHTML);
}

function deleteShape_response(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	document.getElementById('div'+json['id']).outerHTML = json['content'];
	createShape(json['id'],json['javascriptFunction'],0,0,0);
}

function deleteMood_response(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	document.getElementById(json['id']).outerHTML = json['content'];
}


function disableAdmin_response(xhr){
	var json = JSON.parse(xhr.responseText);
	appendErrorMessage(json['errMess']);
	document.getElementById(json['mail']).outerHTML = "";
}